import 'package:animations/animations.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:u9mobile/models/template.dart';
import 'package:u9mobile/widgets/text_group.dart';

import '../blocs/template_bloc.dart';
import '../models/area.dart';
import '../models/filed_property.dart';
/// 子表不折叠
class BillDetail extends StatefulWidget {
  const BillDetail({super.key});
  
  @override
  State<BillDetail> createState() => _BillDetailState();
}

class _BillDetailState extends State<BillDetail> {
  late List<bool> isExpandeds = [false];
  late bool showMore=false;
  /// 渲染表头
  buildCard(Area? area){
    late List<FieldProperty> items=[];
    if(area!=null){
      if(showMore){
        items=area.items;
      }else{
        items=area.items.sublist(0,2);
      }
    }
    return Container(
      margin: const EdgeInsets.fromLTRB(6, 15, 6, 15),
      padding:const EdgeInsets.fromLTRB(10, 5, 10, 5),
      decoration:BoxDecoration(
        color: Colors.white,           
        borderRadius: BorderRadius.circular(10.0),  
        boxShadow: [
          BoxShadow(
            color: Colors.grey.withOpacity(0.5),
            spreadRadius: 3,
            blurRadius: 5,
            offset:const Offset(0, 1),
          ),
        ],
      ),
      child: Column(
        children: [
          Column(
            children: items.map(
                (item) => TextGroup(lebal: item.label,content: '1',)
              ).toList()
          ),
          // const Divider(
          //   height: 1,
          // ),
          ConstrainedBox(
            constraints:const BoxConstraints(minWidth: double.infinity),
            child: TextButton(
              onPressed: () {
                setState(() {
                  showMore=true;
                });
              },
              child: const Text('显示更多'),
            ),
          ),
        ],
      ),
    );
  }

  /// 渲染表体
  buildBodyList(List<Area>? body){
    if(body==null){
      return const SizedBox.shrink();
    }
    return Container(
      padding:const EdgeInsets.all(10),
      child: ExpansionPanelList(
        elevation: 1, // 可选项，设置Card的阴影
        expandedHeaderPadding:const EdgeInsets.all(2), // 可选项，设置展开时标题的内边距
        expansionCallback: (panelIndex, isExpanded) {
          setState(() {
            isExpandeds[0]=!isExpandeds[0];
          });
        },
        children: [
          ExpansionPanel(
            headerBuilder: (context, isExpanded) {
              return const ListTile(
                title: Text('销售明细'),
              );
            },
            body:const Column(
              children: [
                TextGroup(lebal: '编码',content: '1',),
                TextGroup(lebal: '编码',content: '1',),
                TextGroup(lebal: '编码',content: '1',),
                TextGroup(lebal: '编码',content: '1',)
              ],
            ),
            isExpanded: isExpandeds[0]
          )
        ],
      )
    );
  }

  buildBodyDetail(){

  }

  final SnackBar httpToast = const SnackBar(
    content: Text('审批成功',style: TextStyle(color: Colors.white),),
    backgroundColor: Colors.blue,
  );

  @override
  Widget build(BuildContext context) {
    final TemplateBloc templateBloc=BlocProvider.of(context);
    return Column(
      children: [
        Expanded(
          child: SingleChildScrollView(
            child: BlocBuilder<TemplateBloc,Template>(
              builder: (context, state){
                return Column(
                  children: [
                    buildCard(state.card),
                    buildBodyList(state.body)
                  ],
                );
              },
            ),
          ),
        ),
        Padding(
          padding: const EdgeInsets.all(8.0),
          child: Row(
            crossAxisAlignment: CrossAxisAlignment.end,
            mainAxisAlignment: MainAxisAlignment.end,
            children: [
              TextButton(onPressed: () {}, child: const Text('驳回')),
              ElevatedButton(
                style: ElevatedButton.styleFrom(
                  backgroundColor: Colors.blue, // 设置背景颜色
                  foregroundColor: Colors.white, // 设置文本颜色
                ),
                onPressed: () {
                  // ScaffoldMessenger.of(context).showSnackBar(httpToast);
                  templateBloc.fetch();
                },
                child: const Text('审批'),
              ),
            ],
          ),
        )
      ],
    );
  }
}
